﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MDCAdminTool.Commands
{
    public class TabClose : CommandBinding
    {
        public TabClose()
        {
            Command = Application.Current.Resources["TabClose"] as ICommand;
            CanExecute += OnCanExecute;
            Executed   += OnExecuted;
        }

        protected void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var tab = e.Parameter as TabItem;

            if( tab == null )
            {
                var tabControl = e.Source as TabControl;
                
                if ( tabControl != null )
                    tab = tabControl.SelectedItem as TabItem;

                if( tab == null ) return;
            }

            var tc = tab.Parent as TabControl;
            if( tc == null) return;

            tc.Items.Remove( tab );
        }
        protected void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
    }
}